package com.lanou.lenanapp.adapter;

import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.lanou.lenanapp.R;
import com.lanou.lenanapp.beans.JokeReviewHot;
import com.lanou.lenanapp.view.CircleImage;
import com.lanou.lenanapp.volley.VolleySingleton;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class JokeReviewHotAdapter extends BaseAdapter {


    private List<JokeReviewHot> jokeReviewHots;
    private TextView hot_joke_review_author_name, hot_joke_review_created_time,
            hot_joke_review_content, hot_joke_review_likes;
    private CircleImage hot_joke_review_author_avatar;

    VolleySingleton volleySingleton = VolleySingleton.getInstance();

    public void addData(List<JokeReviewHot> datas) {
        jokeReviewHots = datas;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return jokeReviewHots != null && jokeReviewHots.size() > 0 ? jokeReviewHots.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());

            convertView = inflater.inflate(R.layout.activity_review_joke_hot, null);
        }
        hot_joke_review_author_name = (TextView) convertView.findViewById(R.id.hot_joke_review_author_name);
        hot_joke_review_created_time = (TextView) convertView.findViewById(R.id.hot_joke_review_created_time);
        hot_joke_review_content = (TextView) convertView.findViewById(R.id.hot_joke_review_content);
        hot_joke_review_likes = (TextView) convertView.findViewById(R.id.hot_joke_review_likes);
        hot_joke_review_author_avatar = (CircleImage) convertView.findViewById(R.id.hot_joke_review_author_avatar);

        if (jokeReviewHots != null && jokeReviewHots.size() > 0){
            JokeReviewHot jh = jokeReviewHots.get(position);
            hot_joke_review_author_name.setText(jh.getAuthor_name());
            //时间转换格式
            String time = getNormalTime(jh.getCreated_time());
            hot_joke_review_created_time.setText(time);

            hot_joke_review_likes.setText(jh.getLikes());
            hot_joke_review_content.setText(jh.getContent());

            String url = jh.getAuthor_avatar();
            getBitmap(url, hot_joke_review_author_avatar);

        }


        return convertView;
    }

    public  String  getNormalTime( String value) {
        Long l = Long.valueOf(value);
        l = l * 1000;
        Date date = new Date(l);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        String time = format.format(date) ;
        return time;
    }




    private void getBitmap(String url, final ImageView imageView) {
        ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);

            }
        }, 1000, 1000, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        volleySingleton.addRequest(request);
    }
}
